//
//  MaiTangCollectionViewController.m
//  project_delicious
//
//  Created by dlios on 15-3-24.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "MaiTangCollectionViewController.h"
#import "MaiTangTangCollectionViewCell.h"
#import "FoodDetailsViewController.h"
#import "MJRefreshFooterView.h"


@interface MaiTangCollectionViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) NSString *currPage;
@property (nonatomic, retain) NSString *maxPage;
@property (nonatomic, retain) UICollectionView *collection;
@property (nonatomic, retain) NSMutableArray *arr1;
@property (nonatomic, retain) MBProgressHUD *hud;
@end

@implementation MaiTangCollectionViewController
- (void)dealloc
{
    [_hud release];
    [_arr1 release];
    [_collection release];
    [_maxPage release];
    [_currPage release];
    [_reStrP release];
    [_reStrS release];
    [_str release];
    [_arr release];
    [super dealloc];
}
- (NSURL *)strChangeUrl:(NSString *)str
{
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:str];
    return url;
}
//转菊花
- (void)mbHud
{
    self.hud = [[MBProgressHUD alloc] initWithView:self.view];
    self.hud.labelText = @"Loading...";
    //self.hud.detailsLabelText = @"正在为您加载";
    [self.view addSubview:self.hud];
    [_hud release];
    [_hud show:YES];
}
- (void)netWorkRequest:(NSString *)str
{
    self.arr1 = [NSMutableArray array];
    self.navigationController.navigationBar.translucent = NO;
    UIImageView *backImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    backImageView.userInteractionEnabled = YES;
    backImageView.image = [UIImage imageNamed:@"QQ20150406-2.png"];
    [self.view addSubview:backImageView];
    [backImageView release];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray *array = [responseObject objectForKey:@"data"];
        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
        flow.itemSize = CGSizeMake(self.view.frame.size.width / 2, self.view.frame.size.height / 3);
        
        flow.sectionInset = UIEdgeInsetsMake(20, 10, 10, 10);
//        flow.minimumInteritemSpacing = 0;
//        flow.minimumLineSpacing = 0;
        //横向
        //flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        //self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 4, self.view.frame.size.height / 4, self.view.frame.size.width / 2, self.view.frame.size.height * 2 / 5) collectionViewLayout:flow];
        self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flow];
        //self.collection.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"coB.JPG"]];
        self.collection.backgroundColor = [UIColor clearColor];
        self.collection.delegate = self;
        self.collection.dataSource = self;
        self.collection.userInteractionEnabled = YES;
        [backImageView addSubview:self.collection];
        [_collection release];
        [_collection registerClass:[MaiTangTangCollectionViewCell class] forCellWithReuseIdentifier:@"guo"];
        
        self.currPage = [[responseObject objectForKey:@"bean"] objectForKey:@"curPage"];

        self.maxPage = [[responseObject objectForKey:@"bean"] objectForKey:@"maxPageNumber"];
        //        MJRefreshFooterView *foot = [MJRefreshFooterView footer];
        //        foot.frame = CGRectMake(0, 0, 300, 50);
//        [_collection addHeaderWithTarget:self action:@selector(headReAction:)];
        [_collection addFooterWithTarget:self action:@selector(footReAction:)];
        for (NSMutableDictionary *dic in array) {
            [self.arr1 addObject:dic];
        }
        [_hud removeFromSuperview];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"发生错误: %@", error );
    }];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self mbHud];
    [self netWorkRequest:self.str];
    NSLog(@"%@--", self.str);
    
}
//- (void)headReAction:(UICollectionView *)collection
//{
////    self.currPage = @"1";
////    self.str = [[self.reStrP stringByAppendingString:self.currPage] stringByAppendingString:self.reStrS];
////    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
////    self.str = [self.str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
////    [manager GET:self.str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
////        self.arr1 = [NSMutableArray array];
////        self.arr1 = [responseObject objectForKey:@"data"];
////        //NSLog(@"====+++==%ld", self.arr1.count);
////        [self.collection headerEndRefreshing];
////        [self.collection reloadData];
////        
////    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
////        NSLog(@"发生错误: %@", error );
////    }];
//    [self.collection headerEndRefreshing];
//    
//}
- (void)footReAction:(UICollectionView *)collection
{
    
    NSInteger n = [self.currPage integerValue];
    NSInteger maxPage = [self.maxPage integerValue];
    //NSLog(@"currPage:%@", self.currPage);
    //NSLog(@"shuaxin" );
    if (n < maxPage) {
        n = n + 1;
        self.currPage = [NSString stringWithFormat:@"%ld", n];
        self.str = [[self.reStrP stringByAppendingString:self.currPage] stringByAppendingString:self.reStrS];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        self.str = [self.str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSLog(@"132-=-=-=%@", self.str);
        [manager GET:self.str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSMutableArray *arr = [responseObject objectForKey:@"data"];
            for (NSMutableDictionary *dic in arr) {
                [self.arr1 addObject:dic];
            }
            //NSLog(@"====+++==%ld", self.arr1.count);
            [self.collection footerEndRefreshing];
            [self.collection reloadData];
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"发生错误: %@", error );
        }];
    } else {
        [self.collection footerEndRefreshing];
    }
    
    
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arr1.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MaiTangTangCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"guo" forIndexPath:indexPath];
    NSString *str = [[self.arr1 objectAtIndex:indexPath.item] objectForKey:@"imageFilename"];
//    NSLog(@"%@", str);
    [cell.picImageView setImageWithURL:[self strChangeUrl:str]];
    cell.nameLabel.text = [[self.arr1 objectAtIndex:indexPath.item] objectForKey:@"name"];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    FoodDetailsViewController *foodView = [[FoodDetailsViewController alloc] init];
    self.tid = [[[self.arr1 objectAtIndex:indexPath.row] objectForKey:@"id"] integerValue];
    foodView.nameTitle = [[self.arr1 objectAtIndex:indexPath.item] objectForKey:@"name"];
    NSString *str = [NSString stringWithFormat:@"http://121.40.54.251:80/HandheldKitchen/api/more/hotwater!getHotwaterDetail.do?is_traditional=0&id=%ld&phonetype=2", self.tid];
    
    
    foodView.str = str;
    [self.navigationController pushViewController:foodView animated:YES];
    [foodView release];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
